<template lang="pug">
dialogModel(
    ref="dialogLoad"
    :title="title"
    width="460"
    @confirm="confirm()"
)
    formModel(
        :form="form"
        ref="formModelRef"
        :flex="true"
    )
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dialogModel from '@/components/dialogModel.vue'
import formModel from '@/components/formModel.vue'
import apis from '@/apis'
import { success } from '@/utils/message'

const title = ref('新增目录')
const form = ref([])
const formInit = () => {
    form.value = [
        { type: 'input', label: '目录名称', prop: 'nodeName', rules: { required: true } }
    ]
    if(nowType == '编辑'){//修改
        form.value[0].defaultValue = dataInfo.nodeName
    }
}

const dialogLoad = ref(null)
let dataInfo = {}//编辑信息
let nowType = ''
const open = (data, type) =>{
    dataInfo = data
    nowType = type
    result = {}
    formInit()
    dialogLoad.value.open()
}
let result = {}
const formModelRef = ref(null)
const confirm = () => {
    formModelRef.value.validate((type, data) => {
        if (type){
            result = data
            cyDirsaveOne()
        }
    })
}
const emit = defineEmits(['createSuccess']) //

const cyDirsaveOne = () => {//保存
    apis.cyDirsaveOne({
        ...result,
        parentId: nowType == '编辑' ? dataInfo.parentId : dataInfo.id,
        id: nowType == '编辑' ? dataInfo.id : ''
    }).then((res) => {
        if(res.status == 1){
            success(res.msg)
            dialogLoad.value.close()
            emit('createSuccess')
        }else{
            warning(res.msg)
        }
    })

}
defineExpose({
    open
})

</script>

<style lang="less" scoped>
</style>